//
//  XXSQLiteOpe.h   v1.1
//  
//
//  Created by Huang Xiu Yong on 12-7-1.
//  Copyright (c) 2012年 Huang Xiu Yong. All rights reserved.
//
//  Email:rockhxy@gmail.com
//

#import "XXSQLiteOpe.h"

@implementation XXSQLiteOpe

@synthesize filePath=_filePath;

-(void)dealloc{

    [_filePath release];
    [super dealloc];
}


-(id)initWithFilePath:(NSString *)path{

    self=[super init];
    if (self) {
        self.filePath=path;
    }
    return self;
}


-(BOOL)checkDataBaseFile:(NSError **)error{

    if (!_filePath) {
        if (error!=NULL) {
            NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"Data base file path not set",NSLocalizedDescriptionKey, nil];
            *error=[NSError errorWithDomain:kSQLiteDomain code:100 userInfo:dic];
            [dic release];
        }
        return NO;
    }
    NSFileManager *fileManage=[NSFileManager defaultManager];
    
    BOOL isHadFile=[fileManage fileExistsAtPath:_filePath];
    
    if (!isHadFile&&error!=NULL) {
        NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"Data base file not found",NSLocalizedDescriptionKey, nil];
        *error=[NSError errorWithDomain:kSQLiteDomain code:101 userInfo:dic];
        [dic release];
    }
    return [fileManage fileExistsAtPath:_filePath];
}

-(BOOL)handleData:(NSString *)sql parameter:(NSArray *)paraArray error:(NSError **)error{

    BOOL isHadFile=[self checkDataBaseFile:error];
    
    if (!isHadFile) {
        
        return NO;
    }
    
    if (sqlite3_open([_filePath UTF8String], &database)==SQLITE_OK) {
        sqlite3_stmt *stmt=nil;
        
        int success=sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, NULL);
        
        if (success!=SQLITE_OK) {
            if (error!=NULL) {
                NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"Failed to prepare",NSLocalizedDescriptionKey, nil];
                *error=[NSError errorWithDomain:kSQLiteDomain code:102 userInfo:dic];
                [dic release];
            }
           
            return NO;
        }
        
        for (int i=0; i<paraArray.count; i++) {
            NSString *tempParaText=[paraArray objectAtIndex:i];
            sqlite3_bind_text(stmt, i+1, [tempParaText UTF8String], -1, SQLITE_TRANSIENT);
        }
        
        success=sqlite3_step(stmt);
        sqlite3_finalize(stmt);
        sqlite3_close(database);
        
        if (success==SQLITE_ERROR) {
            if (error!=NULL) {
                NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"Execution failure", NSLocalizedDescriptionKey,nil];
                *error=[NSError errorWithDomain:kSQLiteDomain code:103 userInfo:dic];
                [dic release];
            }
            return NO;
        }else {
            return YES;
        }
    }else {
        if (error!=NULL) {
            NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"Failed to open the data base",NSLocalizedDescriptionKey, nil];
            *error=[NSError errorWithDomain:kSQLiteDomain code:104 userInfo:dic];
            [dic release];
        }
        
        return NO;
    }
}

-(BOOL)handleMoreData:(NSString *)sql parameter:(NSArray *)paraArray error:(NSError **)error{

    BOOL isHadFile=[self checkDataBaseFile:error];
    
    if (!isHadFile) {
        
        return NO;
    }
    
    if (sqlite3_open([_filePath UTF8String], &database)==SQLITE_OK) {
        sqlite3_stmt *stmt=nil;
        
        int success=sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, NULL);
        
        if (success!=SQLITE_OK) {
            if (error!=NULL) {
                NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"Failed to prepare",NSLocalizedDescriptionKey, nil];
                *error=[NSError errorWithDomain:kSQLiteDomain code:102 userInfo:dic];
                [dic release];
            }
            
            return NO;
        }
        
        for (int i=0; i<paraArray.count; i++) {
            NSArray *tempParaArray=[paraArray objectAtIndex:i];
            
            for (int j=0; j<tempParaArray.count; j++) {
                sqlite3_bind_text(stmt, j+1, [[tempParaArray objectAtIndex:j] UTF8String], -1, SQLITE_TRANSIENT);
            }
            success=sqlite3_step(stmt);
            if (success==SQLITE_ERROR) {
                if (error!=NULL) {
                    NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"Execution failure", NSLocalizedDescriptionKey,nil];
                    *error=[NSError errorWithDomain:kSQLiteDomain code:103 userInfo:dic];
                    [dic release];
                }
                return NO;
            }
            sqlite3_reset(stmt);
        }
        
        sqlite3_finalize(stmt);
        sqlite3_close(database);
        
        return YES;
    }else {
        if (error!=NULL) {
            NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"Failed to open the data base",NSLocalizedDescriptionKey, nil];
            *error=[NSError errorWithDomain:kSQLiteDomain code:104 userInfo:dic];
            [dic release];
        }
        
        return NO;
    }
}

-(NSArray *)queryData:(NSString *)sql error:(NSError **)error{

    BOOL isHadFile=[self checkDataBaseFile:error];
    
    if (!isHadFile) {
        
        return nil;
    }
    
    if (sqlite3_open([_filePath UTF8String], &database)==SQLITE_OK) {
        
        sqlite3_stmt *stmt=nil;
        
        if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, NULL)==SQLITE_OK) {
            
            NSMutableArray *tempResult=[NSMutableArray new];
            
            while (sqlite3_step(stmt)==SQLITE_ROW) {
                
                NSMutableDictionary *tempDic=[NSMutableDictionary new];
                
                int colNum=sqlite3_data_count(stmt);
                
                for (int i=0; i<colNum; i++) {
                    NSString *tempKey=[NSString stringWithUTF8String: sqlite3_column_name(stmt, i)];
                    NSString *tempValue=[NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, i)];
                    [tempDic setObject:tempValue forKey:tempKey];
                }
                [tempResult addObject:tempDic];
                [tempDic release];
            }
            
            sqlite3_finalize(stmt);
            sqlite3_close(database);
            
            return [tempResult autorelease];
            
        }else {
            
            if (error!=NULL) {
                NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"Execution failure", NSLocalizedDescriptionKey,nil];
                *error=[NSError errorWithDomain:kSQLiteDomain code:103 userInfo:dic];
                [dic release];
            }
            
            return nil;
        }
    }else {
        
        if (error!=NULL) {
            NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"Failed to open the data base",NSLocalizedDescriptionKey, nil];
            *error=[NSError errorWithDomain:kSQLiteDomain code:104 userInfo:dic];
            [dic release];
        }
        
        return nil;
    }
}


-(BOOL)begainHandMoreDataWith:(NSString *)sql{

    
    BOOL isHadFile=[self checkDataBaseFile:nil];
    
    if (!isHadFile) {
        
        return NO;
    }
    
    if (sqlite3_open([_filePath UTF8String], &database)==SQLITE_OK) {
        tempStmt=nil;
        
        int success=sqlite3_prepare_v2(database, [sql UTF8String], -1, &tempStmt, NULL);
        
        if (success!=SQLITE_OK) {
            return NO;
        }else {
            return YES;
        }
    }else {
        return NO;
    }
}

-(BOOL)bindDataWithParaArray:(NSArray *)array index:(NSInteger)index{

    for (int i=0; i<array.count; i++) {
        sqlite3_bind_text(tempStmt, i+1, [[array objectAtIndex:i] UTF8String], -1, SQLITE_TRANSIENT);
    }
    BOOL success=sqlite3_step(tempStmt);
    if (success==SQLITE_ERROR) {

        return NO;
    }
    sqlite3_reset(tempStmt);
    return YES;
}


-(void)endHandMoreData{

    sqlite3_finalize(tempStmt);
    sqlite3_close(database);
}
@end
